import GoEasy from "./goeasy-2.5.11.min.js"

import md5Libs from "@/uview-ui/libs/function/md5";
import Vue from 'vue'




class Util {
	constructor() {

	}

	Md5(str) {
		//MD5
		if (!str) return
		return md5Libs.md5(str)
	}

	Throttle(options) {
		//节流
		if (!options) return
		Vue.prototype.$u.throttle(options.func, options.time = 500)
	}

	/* 生成uuid */
	guid() {
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
			var r = Math.random() * 16 | 0,
				v = c == 'x' ? r : (r & 0x3 | 0x8);
			return v.toString(16);
		})
	}

	/* 生成订单号 */
	orderCode() {
		var orderCode = '';
		for (var i = 0; i < 6; i++) //6位随机数，用以加在时间戳后面。
		{
			orderCode += Math.floor(Math.random() * 10);
		}
		orderCode = new Date().getTime() + orderCode; //时间戳，用来生成订单号。

		return orderCode;
	}

}


class QQMap extends Util {
	constructor() {
		super();
		this.qqMap = Vue.prototype.qqMap;
	}


	/*  搜索
		地点搜索，搜索周边poi，比如：“酒店” “餐饮” “娱乐” “学校” 等等
	*/
	search(options) {
		this.qqMap.search({
			keyword: options.keyword,
			success: options.success,
			fail: options.fail,
			complete: options.complete
		});
	}

	/* 坐标转地址 */
	reverseGeocoder(options) {
		this.qqMap.reverseGeocoder({
			location: options.location,
			success: options.success,
			fail: options.fail,
			complete: options.complete
		})
	}

	/* 路线规划 */
	direction(options) {
		this.qqMap.direction({
			mode: "driving",
			from: options.from,
			to: options.to,
			success: options.success,
			fail: options.fail,
			complete: options.complete
		})
	}


	/* 获取城市列表 */
	getCityList(options) {
		this.qqMap.getCityList({
			success: options.success,
			fail: options.fail,
			complete: options.complete
		})
	}

}

class Goeasy extends Util {
	constructor(goeasy) {
		super();
		this.goeasy = goeasy;
	}



	Connect(options) {
		//建立连接
		this.goeasy.connect({
			id: options.id, //pubsub选填，im必填，最大长度60字符
			data: options.data, //必须是一个对象，pubsub选填，im必填，最大长度300字符，用于上下线提醒和查询在线用户列表时，扩展更多的属性
			onSuccess: options.success,
			onFailed: options.failed,
			onProgress: options.progress
		});
	}

	Disconnect(options) {
		//断开连接
		this.goeasy.disconnect({
			onSuccess: options.onSuccess,
			onFailed: options.onFailed
		});

	}


	getConnectionStatus() {
		/* 判断是否链接 */
		return this.goeasy.getConnectionStatus()
	}

	subscribe(options) {
		/* 订阅推送 */
		this.goeasy.pubsub.subscribe({
			channel: options.channel, //替换为您自己的channel
			onMessage: options.onMessage,
			onSuccess: options.onSuccess,
			onFailed: options.onFailed
		});
	}

	publish(options) {
		/* 通知 */
		this.goeasy.pubsub.publish({
			channel: options.channel, //替换为您自己的channel
			message: options.message, //替换为您想要发送的消息内容
			onSuccess: options.onSuccess,
			onFailed: options.onFailed
		});
	}

}




var util = new Util()
var goEasy = new Goeasy(Vue.prototype.goeasy)
var maps = new QQMap()

export default {
	util,
	goEasy,
	maps
}
